package com.shark.erp.service;

import com.alibaba.fastjson.JSONObject;
import com.shark.erp.entity.Employee;
import com.shark.erp.entity.Goods;
import com.shark.erp.mapper.EmployeeMapper;
import com.shark.erp.mapper.GoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;

    public JSONObject selectAllEmployee() {
        JSONObject result = new JSONObject();
        try {
            List<Employee> employeeList = employeeMapper.selectAllEmployee();
            result.put("code", 0);
            result.put("msg", "查询成功");
            result.put("count", employeeList.size());
            result.put("data", employeeList);
        } catch (Exception ex) {
            result.put("code", 500);
            result.put("msg", "error");
        }
        return result;
    }

    public JSONObject selectEmployeeByName(String name) {
        JSONObject result = new JSONObject();
        try {
            List<Employee> employeeList = employeeMapper.selectEmployeeByName(name);
            result.put("code", 0);
            result.put("msg", "查询成功");
            result.put("count", employeeList.size());
            result.put("data", employeeList);
        } catch (Exception ex) {
            result.put("code", 500);
            result.put("msg", "error");
        }
        return result;
    }

    public JSONObject insertEmployee(Employee employee) {
        JSONObject result = new JSONObject();
        try {
            employeeMapper.insertEmployee(employee);
            result.put("code", 0);
            result.put("msg", "插入成功");
        } catch (Exception ex) {
            result.put("code", 500);
            result.put("msg", "error");
        }
        return result;
    }

    public JSONObject deleteEmployee(String code) {
        JSONObject result = new JSONObject();
        try {
            employeeMapper.deleteEmployee(code);
            result.put("code", 0);
            result.put("msg", "删除成功");
        } catch (Exception ex) {
            result.put("code", 500);
            result.put("msg", "error");
        }
        return result;
    }

    public JSONObject updateEmployee(Employee employee) {
        JSONObject result = new JSONObject();
        try {
            employeeMapper.updateEmployee(employee);
            result.put("code", 0);
            result.put("msg", "修改成功");
        } catch (Exception ex) {
            ex.printStackTrace();
            result.put("code", 500);
            result.put("msg", "error");
        }
        return result;
    }





}
